package com.Cychat.schedule.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.PetOwner;
import com.Cychat.commons.entity.pojo.User;
import com.Cychat.schedule.entity.vo.UserVo;
import com.Cychat.schedule.service.PetOwnerService;
import com.Cychat.schedule.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/schedule/users")
@CrossOrigin
public class PetOwnerController {

    @Autowired
    private PetOwnerService petOwnerService;

    @Autowired
    private UserService userService;

    /**
     * Description : 根据宠物主人ID查询用户信息
     * ChangeLog : 1. 创建 (2025/10/29 10:21 [Lenovo]);
     * @auther: 谢宝辉
     * date: 2025/10/29:10:21
     * @param userId
     * @return com.Cychat.commons.entity.pojo.User
    */
    @GetMapping("/getUserById/{userId}")
    public ResultMessage<UserVo> getUserById(@PathVariable("userId") Long userId) {
        User user = userService.getUserById(userId);
        if (user == null) {
            return ResultMessage.error("用户不存在");
        }
        Integer ownerId = petOwnerService.getPetOwnerIdByUserId(userId);
        if (ownerId == null) {
            return ResultMessage.error("宠物主人不存在");
        }
        PetOwner petOwner = petOwnerService.getPetOwnerById(ownerId);
        if (petOwner == null) {
            return ResultMessage.error("宠物主人不存在");
        }
        UserVo userVo = new UserVo();
        userVo.setUserId(user.getUserId());
        userVo.setPetOwnerId(petOwner.getOwnerId());
        userVo.setNickName(user.getNickName());
        userVo.setAvatarUrl(user.getAvatarUrl());
        userVo.setGender(user.getGender());
        userVo.setCountry(user.getCountry());
        userVo.setProvince(user.getProvince());
        userVo.setCity(user.getCity());
        userVo.setLanguage(user.getLanguage());
        userVo.setPoints(user.getPoints());
        userVo.setVipLevel(user.getVipLevel());
        userVo.setHealthScore(user.getHealthScore());
        userVo.setPetCount(user.getPetCount());
        userVo.setCreateTime(user.getCreateTime());
        return ResultMessage.success(userVo);
    }
}